/*
###########################################################################################################
gloves","jacket","pants","shirt","shoes","skirt","socks","underpants","undershirt","skin","eyes","hair","shape
###########################################################################################################
chest","skull","left shoulder","right shoulder","left hand","right hand","left foot","right foot","spine","
pelvis","mouth","chin","left ear","right ear","left eyeball","right eyeball","nose","r upper arm",
"r forearm","l upper arm","l forearm","right hip","r upper leg","r lower leg","left hip","l upper leg"
,"l lower leg","stomach","left pec","right pec","center 2","top right","top","top left","center"
,"bottom left","bottom","bottom right"
###########################################################################################################
0 4    B  Unlock/Lock an attachment point empty : "@addattach[:<attach_point_name>]=<y/n>"
1 4    B  Unlock/Lock an attachment point full : "@remattach[:<attach_point_name>]=<y/n>"
2 4    B  Unlock/Lock an attachment point : "@detach:<attach_point_name>=<y/n>" 
3 4    B  Allow/prevent editing objects : "@edit=<y/n>"            
4 4    B  Allow/prevent rezzing inventory : "@rez=<y/n>"     
5 4    B  Allow/prevent wearing clothes : @addoutfit[:<part>]=<y/n>     
6 4    B  Allow/prevent removing clothes : @remoutfit[:<part>]=<y/n>   	
7 4    B  Force removing clothes : @remoutfit[:<part>]=force (*)      	
8 4    B  Force removing attachments : @detach[:attachpt]=force (*)     	
4e	 B  Get the list of worn clothes : @getoutfit[:part]=<channel_number>     	
4e   B  Get the list of worn attachments : @getattach[:attachpt]=<channel_number>
########################################################################################################### 
*/
//###########################################################################################################
list clothings =["gloves","jacket","pants","shirt","shoes","skirt","socks",
				 "underpants","undershirt","skin","eyes","hair","shape"];
//###########################################################################################################				 
list attachments=["none","chest","skull","left shoulder","right shoulder","left hand","right hand","left foot"
				,"right foot","spine","pelvis","mouth","chin","left ear","right ear","left eyeball"
				,"right eyeball","nose","r upper arm","r forearm","l upper arm","l forearm","right hip"
				,"r upper leg","r lower leg","left hip","l upper leg","l lower leg","stomach","left pec"
				,"right pec","center 2","top right","top","top left","center","bottom left","bottom"
				,"bottom right"];
//###########################################################################################################				
list att0=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
list att1=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
list att2=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
list cloth5=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
list cloth6=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
list wornClothing=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
list wornAttachment=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
//###########################################################################################################				
integer attachment = TRUE;
integer relaychannel = -1812221819;
string cmdname;
key victim;
list gMenu=[];
list statesMenu=[0,0,0,0,0,0,0,0];
list gAvatarAllow;
key ToucherID;
integer gMenuLink =1337;
integer gDemon =1338;
integer gPlugin =1339;
integer gVic =1329;
integer listen_id;
integer channel_dialog;
integer listen_id2;
integer channel_dialog2=13337;
string msg ="Clothing Option";
list sideMenu=[" ","Back"," "];
integer gSwitch;
string additional;
string additional2;
integer gState;
integer gPage=0;

list pageMenu(list in ,integer page)
{
	list out;
	integer maxpages =llGetListLength(in)/9;
	page = (page)%(maxpages+1);
	out =["<<","[Back]",">>"];
	integer _listcount =(page *9)+9;
	integer _i=_listcount-9;
	
	for (_i;_i<_listcount;_i++)
	{
		if(_i<=llGetListLength(in)&&llList2String(in,_i)!="")
		out+=llList2String(in,_i);
	}
	return out;
}

RLVWornClothing()
{
	gSwitch=1;
	llRlvSay("getoutfit="+(string)channel_dialog2);   
	listen_id2 = llListen( channel_dialog2, "", NULL_KEY, "");

}

RLVWornAttachment()
{
	gSwitch=2;
	llRlvSay("getattach="+(string)channel_dialog2); 
	listen_id2 = llListen( channel_dialog2, "", NULL_KEY, "");
}



Debug(string _text)
{
	llSay(DEBUG_CHANNEL, _text);
}

llRlvSay(string command)
{
	if(attachment==TRUE)
	{
		llOwnerSay("@"+command);
	}
	else
	{
		llSay(relaychannel,cmdname + "," + (string)victim  + "," + "@"+command);
	}
}

MakeRLVState()
{
	if(attachment==TRUE)
	{
	//onRez we make this to be secure if its an attachmentttt
		if(llList2Integer(statesMenu,3)==1)llRlvSay("edit=n");
		if(llList2Integer(statesMenu,4)==1)llRlvSay("rez=n");
		integer _i=0;
		string _part;
		for(_i ;_i < llGetListLength(clothings);_i++)
		{
			_part=llList2String(clothings,_i);
			if(llList2Integer(cloth5,_i)==1)llRlvSay("addoutfit:"+_part+"=n");
			if(llList2Integer(cloth6,_i)==1)llRlvSay("remoutfit:"+_part+"=n");
		}
		_i=1;
		for(_i ;_i < llGetListLength(attachments);_i++)
		{
			_part=llList2String(attachments,_i);
			if(llList2Integer(att0,_i)==1)llRlvSay("addattach:"+_part+"=n");
			if(llList2Integer(att1,_i)==1)llRlvSay("remattach:"+_part+"=n");
			if(llList2Integer(att2,_i)==1)llRlvSay("detach:"+_part+"=n");
		}
	}
}

llLRP(integer element_new, integer placeinlist)
{
	statesMenu = llListReplaceList(statesMenu, [element_new], placeinlist, placeinlist);
}

list llLRPE(list parts, integer element_new, integer placeinlist)
{
	parts = llListReplaceList(parts, [element_new], placeinlist, placeinlist);
	return parts;
}

makeMenu()
{
	gState=9;
	gMenu=[];
	gMenu+=sideMenu;
	gMenu+=["Lock Empty","Lock Full","Lock Point"];
	gMenu+=["Wearing","Removing","Rem. Clothing","Rem. Attachment"];
	if(llList2Integer(statesMenu,3)==1)gMenu+="[x]Edit Object";
	else gMenu+="[ ]Edit Object";
	if(llList2Integer(statesMenu,4)==1)gMenu+="[x]Rez Object";
	else gMenu+="[ ]Rez Object";
	//Than display
	channel_dialog = ( -1 * (integer)("0x"+llGetSubString((string)llGetKey(),-5,-1)) );
	listen_id = llListen( channel_dialog, "", NULL_KEY, "");
	llDialog(ToucherID, msg,gMenu, channel_dialog);
	llSetTimerEvent(45.0);
}

displayMenu()
{
	string msg ="Clothing Menu";
	list _menu;
	list _manipulation; 
	if(gState==0){_manipulation=att0;gMenu=attachments;}
	else if(gState==1){_manipulation=att1;gMenu=attachments;}
	else if(gState==2){_manipulation=att2;gMenu=attachments;}
	else if(gState==5){_manipulation=cloth5;gMenu=clothings;}
	else if(gState==6){_manipulation=cloth6;gMenu=clothings;}
	else if(gState==7){_manipulation=wornClothing;gMenu=clothings;}
	else if(gState==8){_manipulation=wornAttachment;gMenu=attachments;}
	if(gState==0 || gState ==1 || gState==2 || gState ==5 || gState ==6)
	{
		integer _i=0;
		list _replace=[];
		for(_i;_i<llGetListLength(gMenu);_i++)
		{
			if(llList2Integer(_manipulation,_i)==1)
			{
				string _ListItem=llList2String(gMenu,_i);
				_replace+="[+]"+_ListItem;
			}
			else
			{
				string _ListItem=llList2String(gMenu,_i);
				_replace+=_ListItem;
			}
		}
		gMenu=_replace;
	}
	else if(gState==7 || gState ==8)
	{
		integer _i=0;
		list _replace=[];
		for(_i;_i<llGetListLength(gMenu);_i++)
		{
			if(llList2Integer(_manipulation,_i)==1)
			{
				string _ListItem=llList2String(gMenu,_i);
				_replace+=_ListItem;
			}
		}
		gMenu=_replace;
	}
	if(llGetListLength(gMenu)<10)
	{
		_menu= [" ","[Back]", " "]+gMenu;
	}
	else
	{
		_menu= pageMenu(gMenu,gPage);
	}
	channel_dialog = ( -1 * (integer)("0x"+llGetSubString((string)llGetKey(),-5,-1)) );
	listen_id = llListen( channel_dialog, "", NULL_KEY, "");
	llDialog(ToucherID, msg,_menu, channel_dialog);
	llSetTimerEvent(45.0);
}




default {
	
    state_entry() 
    {
    	    
    }
    
    listen(integer channel, string name, key id, string message)
    {
		if(channel==channel_dialog)
		{
			llListenRemove(listen_id);
			llSetTimerEvent(0.0);
			if(~llListFindList(gMenu, (list)message) && gState !=9)
	  		{
	  			if(gState==0 ||gState==1 ||gState==2 ||gState==5 ||gState==6 || gState ==7 || gState ==8)
	  			{
	  				if(message!="[Back]")
	  				{
	  					integer index;
	  					integer _on=1;
	  					string _part;
	  					string _additional="n";
	  					if(gState==0 ||gState==1 ||gState==2 || gState ==8)
	  					{
	  						string firstChar = llGetSubString(message, 0, 0);
	  						if(firstChar =="["){message=llGetSubString(message,3,100); _on=0;_additional="y";}
	  						index = llListFindList(attachments, [message]);
	  						_part=llList2String(attachments,index);
	  					}
	  					if(gState==5 ||gState==6 || gState ==7)
	  					{
	  						string firstChar = llGetSubString(message, 0, 0);
	  						if(firstChar =="["){message=llGetSubString(message,3,100);_on =0;_additional="y";}
	  						index = llListFindList(clothings, [message]);
	  						_part=llList2String(clothings,index);
	  					}
	  					if(gState==0){att0=llLRPE(att0,_on,index);llRlvSay("addattach:"+_part+"="+_additional);}
	  					if(gState==1){att1=llLRPE(att1,_on,index);llRlvSay("remattach:"+_part+"="+_additional);}
	  					if(gState==2){att2=llLRPE(att2,_on,index);llRlvSay("detach:"+_part+"="+_additional);}
	  					if(gState==5){cloth5=llLRPE(cloth5,_on,index);llRlvSay("addoutfit:"+_part+"="+_additional);}
	  					if(gState==6){cloth6=llLRPE(cloth6,_on,index);llRlvSay("remoutfit:"+_part+"="+_additional);}
	  					if(gState==7){wornClothing=llLRPE(wornClothing,0,index);llRlvSay("remoutfit:"+_part+"=force");}
	  					if(gState==8){wornAttachment=llLRPE(wornAttachment,0,index);llRlvSay("detach:"+_part+"=force");}
	  					displayMenu();
	  				}
	  			}
	  		}
	  		if(message=="Back")llMessageLinked(LINK_THIS, gMenuLink, "DV_MAIN_RLV", ToucherID);
    		if(message=="[x]Edit Object"){llLRP(0,3);llRlvSay("edit=y");makeMenu();}
			if(message=="[ ]Edit Object"){llLRP(1,3);llRlvSay("edit=n");makeMenu();}
    		if(message=="[x]Rez Object"){llLRP(0,4);llRlvSay("rez=y");makeMenu();}
			if(message=="[ ]Rez Object"){llLRP(1,4);llRlvSay("rez=n");makeMenu();}		
			if(message=="Lock Empty")
			{
				gState=0;gPage=0;displayMenu();
			}
			if(message=="Lock Full")
			{
				gState=1;gPage=0;displayMenu();
			}
			if(message=="Lock Point") 
			{
				gState=2;gPage=0;displayMenu();
			}
			if(message=="Wearing")
			{
				gState=5;gPage=0;displayMenu();
			}
			if(message=="Removing")
			{
				gState=6;gPage=0;displayMenu();
			}
			if(message=="Rem. Clothing")
			{
				gPage=0;gState=7;RLVWornClothing();
			}
			if(message=="Rem. Attachment")
			{
				gPage=0;gState=8;RLVWornAttachment();
			}
			if(message=="[Back]"){makeMenu();}
			if(message=="<<")
	  		{
	  			gPage--;
				integer maxpages =llGetListLength(gMenu)/9;
				if(gPage<0)
				{
					gPage = maxpages;
				}
				displayMenu();
	  		}
	  		else if(message==">>")
	  		{
	  			gPage++;
	  			displayMenu();
	  		}
		}
		if(channel==channel_dialog2)
		{
			llListenRemove(listen_id2);
			llSetTimerEvent(0.0);
			if(gSwitch==1)
			{
				integer _i=0;
				integer _count = llStringLength(message);
				for(_i;_i<_count;_i++)
				{
					integer  exchange=(integer)llGetSubString(message,_i,_i);
					wornClothing = llListReplaceList(wornClothing, [exchange], _i, _i);
				}
				displayMenu();
			}
			else if(gSwitch==2)
			{
				integer _i=0;
				integer _count = llStringLength(message);
				for(_i;_i<_count;_i++)
				{
					integer  exchange=(integer)llGetSubString(message,_i,_i);
					wornAttachment = llListReplaceList(wornAttachment, [exchange], _i, _i);
				}
				displayMenu();
			}
			
		}
    }
    
    on_rez(integer on)
    {
    
    }
    
    link_message(integer sender, integer num, string str, key id)
  	{
  		if(num ==gMenuLink)
  		{
  			if(str==llGetScriptName())
  			{
  				llMessageLinked(LINK_THIS, gDemon, llGetScriptName(), NULL_KEY);
  				ToucherID=id;
  				makeMenu();
  			}
  		}
  		if(num ==gDemon)
  		{
  			if(str =="attachment")attachment=TRUE;
  			else if(str =="device")attachment=FALSE;
  			else if(str!=llGetScriptName())
  			{
				llListenRemove(listen_id);
				llListenRemove(listen_id2);
  			}
  		}
  		if(num==gPlugin)
  		{
  			if(str=="ON_REZ")
  			{
  				MakeRLVState();
  			}
  			if(str=="RESET")
  			{
  				att0=[0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
				att1=[0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
				att2=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
				cloth5=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
				cloth6=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
				wornClothing=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
				wornAttachment=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
				statesMenu=[0,0,0,0,0,0,0,0];
				
  			}
  		}
  		if(num==gVic)
  		{
  				victim=(key)str;
  				cmdname = (string)llGetKey();
  		}
  	}
  	
  	timer()
    {
    	llSetTimerEvent(0.0);
    	llListenRemove(listen_id);
    	llListenRemove(listen_id2);
   	
    }
}
